<template>
    <el-header>
        <div class="header">
            <div class="user-info">
                <el-dropdown type="text">
                    <div class="avatar">
                        <img :src="user.avatar" alt="" />
                    </div>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item>
                                <el-button
                                    @click="logoutdialog = true"
                                    type="danger"
                                    size="small"
                                >
                                    <el-icon>
                                        <User />
                                    </el-icon>
                                    退出登录
                                </el-button>
                            </el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
                <div class="detail">
                    <h4>欢迎您，{{ user.username }}</h4>
                    <p>上次登录：{{ user.lastLogin }}</p>
                </div>
            </div>
        </div>
    </el-header>
    <Teleport to="body">
        <!-- 确定退出 -->
        <el-dialog v-model="logoutdialog" align-center title="提示" width="360">
            <div>确定要退出登录吗？</div>
            <template #footer>
                <el-button @click="logoutdialog = false">取消</el-button>
                <el-button type="primary" @click="logoutHandle">确定</el-button>
            </template>
        </el-dialog>
    </Teleport>
</template>

<script setup>
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import { User } from "@element-plus/icons-vue";
import { ref } from "vue";
import { UserLogout } from "@/api/users";
import { ClearCookie } from "@/utils/cookie";

const router = useRouter();
const logoutdialog = ref(false);
const { user } = defineProps({
    user: { type: Object, required: true }
});

// 用户登出
const logoutHandle = async () => {
    logoutdialog.value = false;
    const res = await UserLogout();
    if (res) {
        ClearCookie("userid");
        ElMessage.success("登出成功");
        router.replace("/login");
    }
};
</script>

<style lang="scss" scoped>
.header {
    display: flex;
    align-items: center;
    background: #fff;
    position: relative;
    height: 100%;
    width: calc(100% + 20px);
    .user-info {
        margin-left: auto;
        display: flex;
        align-items: center;
        padding-right: 20px;
        .avatar {
            width: 36px;
            height: 36px;
            display: flex;
            justify-content: center;
            align-items: center;
            border-radius: 50%;
            overflow: hidden;
            border: 1px solid #ff0000;
            margin-right: 10px;
            img {
                width: 100%;
            }
        }
        .detail {
            h4 {
                font-size: 14px;
                color: #333;
                margin-bottom: 4px;
            }
            p {
                font-size: 12px;
                color: #6b6868;
            }
        }
    }
}
</style>
